package com.elcl.support.versionupdate;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.elcl.network.AppConstant;
import com.elcl.util.StringUtils;


/**
 * Created by lichunlin on 17/6/14.
 * 版本下载的初始类
 * 1、如果是开启wifi自动下载的话，当开始start的时候，检测当前环境是否为wifi，
 * 1.1：是的话，开启下载，如果在下载的过程中，网络变为非wifi的状况下，停止下载（停止服务），下载成功后，回调downloadSuccess，type为2
 * 1.2：否则的话，停止下载（停止服务），回调downloadfail
 * 2、如果没有开启wifi自动下载，则开始下载，下载成功，回调downloadSuccess，type为1
 * <p>
 * a、下载过程，杀死应用进程后，再次进入，重新去下载（断点下载，需要服务器支持，暂时注释掉）
 * b、下载后，先判断本地的apk的包是否网络的包是否一样大，如果一样大，直接提示安装，回调fileAlreadyDownload
 * c、安装后，删除安装包
 * <p>
 * todo:1、网络切换的时候，下载停止，2、杀死app进程后，服务停止
 */

public class VersionDownloadHelper {

    public VersionDownloadHelper(Context context) {
        this.context = context;
    }

    private String downloadFilePath = "";//下载apk存放的地址
    private String downloadUrl = "";//下载的地址
    private String appName = "";//app的名称(名称+版本名称)
    private boolean isOpenWifiDownload = false;//是否开启wifi自动下载
    private Context context;
    private int downloadIcon;

    private NotificationManager notificationManager;

    private VersionDownloadHelper(Builder builder) {
        this.context = builder.context;
        this.downloadFilePath = builder.downloadFilePath;
        this.downloadUrl = builder.downloadUrl;
        this.appName = builder.appName;
        this.downloadIcon = builder.downloadIcon;
        this.isOpenWifiDownload = builder.isOpenWifiDownload;

        notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
    }

    private static VersionDownLoadListener downLoadListener;

    //开始version的下载
    public void startVersionDownload(VersionDownLoadListener downLoadListener) {
        this.downLoadListener = downLoadListener;
        if (StringUtils.isNotBlank(downloadUrl)) {
            Intent intent = new Intent(context, DownloadService.class);
            intent.putExtra(AppConstant.KEY_APP_NAME, appName);
            intent.putExtra(AppConstant.KEY_DOWN_URL, downloadUrl);
            intent.putExtra(AppConstant.KEY_APP_ICON, downloadIcon);
            intent.putExtra(AppConstant.KEY_FILE_PATH, downloadFilePath);
            intent.putExtra(AppConstant.KEY_WIFI_OPEN, isOpenWifiDownload);
            context.startService(intent);
        }
    }


    /**
     * 下载的广播接收器
     */
    public static class DownLoadReceiver extends BroadcastReceiver {

        public DownLoadReceiver() {

        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (null != intent) {
                String type = intent.getStringExtra(AppConstant.KEY_BROCASTER_TYPE);
                switch (type) {
                    case AppConstant.FILE_ALRADY_DOWNLOAD:
                        downLoadListener.fileAlreadyDownload();
                        break;
                    case AppConstant.DOWNLOAD_PROGRESS:
                        int progress = intent.getIntExtra("progress", 0);
                        downLoadListener.downloadProgress(progress);
                        break;
                    case AppConstant.DOWNLOAD_SUCCESS://下载成功
                        byte download_type = intent.getByteExtra(AppConstant.KEY_BROCASTER_SUCCESS_TYPE, (byte) 0);
                        downLoadListener.downloadSuccess(download_type);
                        break;
                    case AppConstant.DOWNLOAD_FAILED:
                        downLoadListener.downloadFailed();
                        break;
                }
            }
        }
    }

    public static class Builder {
        private String downloadFilePath = "";//下载apk存放的地址
        private String downloadUrl = "";//下载的地址
        private boolean isOpenWifiDownload = false;//是否开启wifi自动下载
        private String appName = "";//app的名称
        private int downloadIcon = 0;//下载的时候展示的icon
        private Context context;

        public Builder(Context ctx) {
            this.context = ctx;
        }

        /**
         * 下载apk文件的在本地存放的地址
         *
         * @return
         */
        public Builder downloadFilePath(String downloadFilePath) {
            this.downloadFilePath = downloadFilePath;
            return this;
        }

        /**
         * 下载apk文件网络地址
         *
         * @return
         */
        public Builder downloadUrl(String downloadUrl) {
            this.downloadUrl = downloadUrl;
            return this;
        }

        /**
         * app的名称
         *
         * @return
         */
        public Builder appName(String appName) {
            this.appName = appName;
            return this;
        }

        /**
         * 下载apk文件，通知栏的图标
         *
         * @return
         */
        public Builder downloadIcon(int downloadIcon) {
            this.downloadIcon = downloadIcon;
            return this;
        }

        /**
         * 下载apk文件，通知栏的图标
         *
         * @return
         */
        public Builder isOpenWifiDownload(boolean isOpenWifiDownload) {
            this.isOpenWifiDownload = isOpenWifiDownload;
            return this;
        }

        /**
         * 构造UpdateManager对象
         *
         * @return
         */
        public VersionDownloadHelper build() {
            return new VersionDownloadHelper(this);
        }
    }


}
